package com.qianfeng.controller;

import com.qianfeng.dto.R;
import com.qianfeng.log.annotations.LogAnnotation;
import com.qianfeng.pojo.SysCategory;
import com.qianfeng.service.CategoryService;
import com.qianfeng.valigroup.Insert;
import com.qianfeng.valigroup.UpDate;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/6 17:54
 */
@RestController
@RequestMapping("/categories")
@Api(tags = "分类管理模块")
public class CategoryController {
    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    @PostMapping("/category")
    @LogAnnotation(title = "添加分类",business_type = 1)
    public R addCategory(@RequestBody @Validated(Insert.class)SysCategory sysCategory){
        categoryService.addCategory(sysCategory);
        return R.OK();
    }
    @GetMapping("")
    @LogAnnotation(title = "查询所有分类",business_type = 0)
    public R findAllCategory(){
        return R.OK(categoryService.findAllCategoris());
    }

   @GetMapping("/category/{cid}")
   @LogAnnotation(title = "查询分类",business_type = 0)
    public R findById(@PathVariable Long cid){
        SysCategory category=categoryService.findById(cid);
        return R.OK(category);
    }
    @PutMapping("/category")
    @LogAnnotation(title = "修改分类",business_type = 2)
    public R updateCategory(@Validated(UpDate.class) @RequestBody SysCategory category){
        System.out.println("cid"+category.getCid());
        categoryService.updateCategory(category);
        return R.OK();
    }
    @DeleteMapping("/category/{cid}")
    @LogAnnotation(title = "删除分类",business_type = 3)
    public R deleteById(@PathVariable Long cid){
        categoryService.deleteById(cid);
        return R.OK();
    }
}
